package com.xz.coursearrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.coursearrange.common.ServerResponse;
import com.xz.coursearrange.common.validation.AddGroup;
import com.xz.coursearrange.common.validation.UpdateGroup;
import com.xz.coursearrange.entity.ClassInfo;
import com.xz.coursearrange.entity.Student;
import com.xz.coursearrange.entity.request.ClassAddVO;
import com.xz.coursearrange.service.ClassInfoService;
import com.xz.coursearrange.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * 班级信息
 */
@RestController
@Validated  // 必须加这个，才能对 @PathVariable、@RequestParam 等进行校验
public class ClassInfoController {

    @Resource
    private ClassInfoService classInfoService;
    @Resource
    private StudentService studentService;


    /**
     * 根据年级查询所有班级
     */
    @GetMapping("/class-grade/{grade}")
    public ServerResponse queryClass(@PathVariable("grade") @NotBlank(message = "年级不能为空") String grade) {
        QueryWrapper<ClassInfo> wrapper = new QueryWrapper<ClassInfo>().eq("remark", grade);
        List<ClassInfo> classInfoList = classInfoService.list(wrapper);

        return ServerResponse.ofSuccess(classInfoList);
    }

    /**
     * 根据班级查询学生
     */
    @GetMapping("/student-class/{page}/{classNo}")
    public ServerResponse queryStudentByClass(@PathVariable("page")
                                              @Min(value = 1, message = "页码必须大于等于1")
                                              Integer page,
                                              @PathVariable("classNo")
                                              @NotBlank(message = "班级编号不能为空")
                                              String classNo,
                                              @RequestParam(defaultValue = "10")
                                              @Min(value = 1, message = "每页数量必须大于0")
                                              @Max(value = 100, message = "每页最多100条")
                                              Integer limit) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("student_no");
        wrapper.like(!StringUtils.isEmpty(classNo), "class_no", classNo);
        Page<Student> pages = new Page<>(page, limit);
        IPage<Student> iPage = studentService.page(pages, wrapper);
        return ServerResponse.ofSuccess(iPage);
    }

    /**
     * 查询班级信息带详细信息
     */
    @GetMapping("/queryclassinfo/{page}")
    public ServerResponse queryClassInfos(@PathVariable("page")
                                          @Min(value = 1, message = "页码必须大于等于1")
                                          Integer page,
                                          @RequestParam(defaultValue = "10")
                                          @Min(value = 1, message = "每页数量必须大于0")
                                          @Max(value = 100, message = "每页最多查询100条")
                                          Integer limit,
                                          @RequestParam(required = false)
                                          @Size(max = 10, message = "年级编号长度不能超过10")
                                          String gradeNo) {
        return classInfoService.queryClassInfos(page, limit, gradeNo);
    }


    /**
     * 添加班级
     */
    @PostMapping("/classinfo/add")
    public ServerResponse addClass(@RequestBody @Validated(AddGroup.class) ClassAddVO classAddVO) {
        ClassInfo c = new ClassInfo();
        BeanUtils.copyProperties(classAddVO, c);
        c.setRemark(classAddVO.getGradeNo());

        c.setTeacher(classAddVO.getId());
        return classInfoService.save(c) ? ServerResponse.ofSuccess("添加班级成功") : ServerResponse.ofError("添加班级失败");
    }

    /**
     * 更新班级信息
     */
    @PostMapping("/classinfo/modify/{id}")
    public ServerResponse modifyClassInfo(@PathVariable("id")
                                          @NotNull(message = "ID不能为空")
                                          Integer id,
                                          @RequestBody
                                          @Validated(UpdateGroup.class)
                                          ClassAddVO classAddVO) {
        ClassInfo classInfo = new ClassInfo();
        BeanUtils.copyProperties(classAddVO, classInfo);
        classInfo.setRemark(classAddVO.getGradeNo());

        QueryWrapper<ClassInfo> wrapper = new QueryWrapper<ClassInfo>().eq("id", id);
        return classInfoService.update(classInfo, wrapper) ? ServerResponse.ofSuccess("更新成功") : ServerResponse.ofError("更新失败");
    }

    /**
     * 根据ID删除教材信息
     */
    @DeleteMapping("/classinfo/delete/{id}")
    public ServerResponse deleteClassInfoById(@PathVariable("id")
                                              @NotNull(message = "ID不能为空")
                                              Integer id) {
        return classInfoService.removeById(id) ? ServerResponse.ofSuccess("删除成功") : ServerResponse.ofError("删除失败");
    }
}

